package com.aochen.demo0419;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;
import android.view.ViewGroup;

import com.aochen.demo0419.helper.ItemDragHelperCallback;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ArrayList<String> mTitleDataList;

    RecyclerView rv;
    private String[] dingDan = new String[]{"个位定胆", "十位定胆", "百位定胆", "千位定胆", "万位定胆"};
    private String[] erXing = new String[]{"前二单式", "前二复式", "前二组选复式", "后二复式", "后二单式", "后二组选复式"};
    private String[] sanXing = new String[]{"前三复式", "前三单式", "前三组三复式", "前三组六复式",
            "中三复式", "中三单式", "中三组三复式", "中三组六复式", "后三复式", "后三单式", "后三组三复式", "后三组六复式"};
    private String[] siXing = new String[]{"后四组选24", "后四组选12", "后四复式", "后四单式"};
    private String[] wuXing = new String[]{"五星组选120", "五星组选60", "五星复式", "五星单式"};
    //切换玩法
    private ArrayList<String[]> wanFa;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
    }

    private void init() {

        mTitleDataList = new ArrayList<>();
        mTitleDataList.add("定胆");
        mTitleDataList.add("二星");
        mTitleDataList.add("三星");
        mTitleDataList.add("四星");
        mTitleDataList.add("五星");

        wanFa = new ArrayList<>();
        wanFa.add(dingDan);
        wanFa.add(erXing);
        wanFa.add(sanXing);
        wanFa.add(siXing);
        wanFa.add(wuXing);

        final ViewPager mViewPager = findViewById(R.id.vp);
        mViewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return mTitleDataList.size();
            }

            @Override
            public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
                return view == object;
            }

            @NonNull
            @Override
            public Object instantiateItem(@NonNull ViewGroup container, int position) {
                View view = View.inflate(MainActivity.this, R.layout.container, null);

                rv = view.findViewById(R.id.rv);
                initRv(position);
                container.addView(view);
                return view;
            }

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
                container.removeView((View) object);
            }
        });
        MagicIndicator magicIndicator = (MagicIndicator) findViewById(R.id.magic_indicator);
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdjustMode(true);  //ture 即标题平分屏幕宽度的模式
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {

            @Override
            public int getCount() {
                return mTitleDataList == null ? 0 : mTitleDataList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                ColorTransitionPagerTitleView colorTransitionPagerTitleView = new ColorTransitionPagerTitleView(context);
                colorTransitionPagerTitleView.setNormalColor(Color.BLACK);
                colorTransitionPagerTitleView.setSelectedColor(Color.RED);
                colorTransitionPagerTitleView.setText(mTitleDataList.get(index));
                colorTransitionPagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mViewPager.setCurrentItem(index);
                    }
                });
                return colorTransitionPagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
                indicator.setColors(Color.RED);
                return indicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);

        ViewPagerHelper.bind(magicIndicator, mViewPager);
    }

    private void initRv(int position) {
        final List<ChannelEntity> items = new ArrayList<>();

        String[] current = wanFa.get(position);

        for (int i = 0; i < current.length; i++) {
            ChannelEntity entity = new ChannelEntity();
            entity.setName(current[i]);
            items.add(entity);
        }
        final List<ChannelEntity> otherItems = new ArrayList<>();
        // TODO: 2018/4/20 0020 上半部分颜色设置红色
        if (position==0){
            ChannelEntity entity = new ChannelEntity();
            entity.setName(current[0]);
            otherItems.add(entity);
        }


        GridLayoutManager manager = new GridLayoutManager(this, 4);
        rv.setLayoutManager(manager);

        ItemDragHelperCallback callback = new ItemDragHelperCallback();
        final ItemTouchHelper helper = new ItemTouchHelper(callback);
        helper.attachToRecyclerView(rv);

        final ChannelAdapter adapter = new ChannelAdapter(this, helper, items, otherItems);
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                int viewType = adapter.getItemViewType(position);
                return viewType == ChannelAdapter.TYPE_MY || viewType == ChannelAdapter.TYPE_OTHER ? 1 : 4;
            }
        });
        rv.setAdapter(adapter);
    }
}
